package org.skill.panel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.Frame.MsgJframe;
import org.come.Jpanel.GameJpanel;
import org.come.action.MapAction;
import org.come.bean.Skill;
import org.come.until.*;
import org.skill.frame.SkillMainFrame;

import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



/**
 * 师门左侧技能栏
 * 
 * @author Administrator
 * 
 */
public class SkillSMPanel extends JPanel {
    private JLabel skillTitle, // 技能标题
            skillSects, // 技能门派
            skillAddress; // 技能门派地址
    private JLabel[] skillBar // 技能栏
            ,
            skillValue // 技能值
            , skillArt // 技能条
            , skillskilimg // 技能图片
            , skillName // 技能名称
            , skillShelter // 技能遮挡
            ;
    /** 熟练度最大值 */
    private int proficiencyMax = 0;

    public SkillSMPanel(int py) {
        if("水墨UI".equals(MyIsif.getStyle())) {
            this.setBounds(176 * py, 0, 170, 300);
            this.setOpaque(false);
            this.setLayout(null);
            // 生成有下划线的字体
            HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
            hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); // 定义是否有下划线
            hm.put(TextAttribute.SIZE, 12); // 定义字号
            hm.put(TextAttribute.FAMILY, "Simsun"); // 定义字体名
            Font fontAddr = new Font(hm);
            // 组件初始化
            skillTitle = new JLabel("速");
            skillSects = new JLabel("");//门派:
            skillAddress = new JLabel("");//狮门岭
            skillBar = new JLabel[5];
            skillValue = new JLabel[5];
            skillArt = new JLabel[5];
            skillName = new JLabel[5];
            skillskilimg = new JLabel[5];
            // skillClick = new JLabel[5];
            skillShelter = new JLabel[5];
            for (int i = 0; i < 5; i++) {
                skillBar[i] = new JLabel();
                skillValue[i] = new JLabel(i + "/100000");
                skillArt[i] = new JLabel();
                skillShelter[i] = new JLabel("点击学习该技能");
                skillName[i] = new JLabel("技能名称");
                skillskilimg[i] = new JLabel();
            }

            // 组件设置字体颜色
            skillTitle.setForeground(new Color(251, 216, 51));
            skillSects.setForeground(new Color(251, 216, 51));
            skillAddress.setForeground(new Color(0, 255, 0));
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setForeground(Color.white);
                skillName[i].setForeground(Color.white);
                skillShelter[i].setForeground(Color.green);
            }

            // 组件设置字体大小样式
            skillTitle.setFont(UIUtils.TEXT_HY34);
            skillSects.setFont(new Font("宋体", 0, 12));
            skillAddress.setFont(fontAddr);
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setFont(new Font("宋体", 0, 12));
                skillName[i].setFont(UIUtils.TEXT_HY16);
                skillShelter[i].setFont(UIUtils.TEXT_HY14);
            }

            // 设置图片
            for (int i = 0; i < 5; i++) {
                skillBar[i].setIcon(CutButtonImage.getImage("inkImg/old/background/55.png", 108, 15));
                skillArt[i].setIcon(CutButtonImage.getImage("skin/8100226/button/27.png", 101, 10));
            }

            // 组件设置大小和方位
            skillTitle.setBounds(0, 0, 170, 40);
            skillSects.setBounds(45, 35, 30, 15);
            skillAddress.setBounds(75, 35, 90, 15);
            for (int i = 0; i < 5; i++) {
                skillBar[i].setBounds(51, 82 + i * 48, 108, 15);
                skillArt[i].setBounds(55, 84 + i * 48, 101, 10);
                skillValue[i].setBounds(53, 81 + i * 48, 103, 12);
                skillName[i].setBounds(51, 62 + i * 49, 103, 14);
                skillShelter[i].setBounds(45, 61 + i * 49, 119, 39);
                skillskilimg[i].setBounds(10, 61 + i * 49, 40, 40);
            }

            // 组建设置透明
            skillTitle.setOpaque(false);
            skillSects.setOpaque(false);
            skillAddress.setOpaque(false);
            for (int i = 0; i < 5; i++) {
                skillBar[i].setOpaque(false);
                skillArt[i].setOpaque(false);
                skillValue[i].setOpaque(false);
                skillName[i].setOpaque(false);
                skillShelter[i].setOpaque(false);
                skillskilimg[i].setOpaque(false);
            }
            // 内容居中
            skillTitle.setHorizontalAlignment(SwingConstants.CENTER);
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setHorizontalAlignment(SwingConstants.CENTER);
                skillShelter[i].setHorizontalAlignment(SwingConstants.CENTER);
            }

            for (int i = 0; i < skillShelter.length; i++) {
                skillShelter[i].setVisible(true);
            }
            // 添加组件
            this.add(skillTitle);
            this.add(skillSects);
            this.add(skillAddress);
            for (int i = 0; i < 5; i++) {
                this.add(skillName[i]);
                this.add(skillValue[i]);
                this.add(skillArt[i]);
                this.add(skillBar[i]);
                this.add(skillShelter[i]);
                this.add(skillskilimg[i]);
            }
            skillAddress.addMouseListener(new MouseAdapter() {
                Cursor cursor = new Cursor(Cursor.HAND_CURSOR);

                @Override
                public void mouseEntered(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseEntered(e);
                    JLabel source = (JLabel) e.getSource();
                    source.setCursor(cursor);
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseClicked(e);
                    JLabel source = (JLabel) e.getSource();
                    source.setCursor(cursor);
                }
            });
            class MListener extends MouseAdapter {
                private int numType;

                public MListener(int numType) {
                    this.numType = numType;
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (!skillName[numType].isVisible()) {
                        MapAction.npcmenuAction.get(NpcMenuUntil.SKILL).menuControl(skillName[numType].getText());
                        SkillMainFrame.getSkillMainFrame().getSkillMainPanel().changeBtnPanel(0);
                    }
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    if (skillName[numType].isVisible()) {
                        Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                        if (skill == null) {
                            return;
                        }
                        MsgJframe
                                .getJframe()
                                .getJapnel()
                                .SM(skill, Double.parseDouble(skillValue[numType].getText().split("/")[0]),
                                        AnalysisString.lvlint(RoleData.getRoleData().getLoginResult().getGrade()));
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    FormsManagement.HideForm(46);
                }
            }

//            for (int i = 0; i < skillShelter.length; i++) {
//                MListener l = new MListener(i);
//                skillShelter[i].addMouseListener(l);
//                skillName[i].addMouseListener(l);
//
//            }
            for (int i = 0; i < skillShelter.length; i++) {
                MListener l = new MListener(i);
                skillShelter[i].addMouseListener(l);
                skillName[i].addMouseListener(l);
                skillBar[i].addMouseListener(l);
                skillArt[i].addMouseListener(l);
                skillValue[i].addMouseListener(l);
            }
            class MListener1 extends MouseAdapter {
                private int numType;

                public MListener1(int numType) {
                    this.numType = numType;
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                    GameJpanel.skill = skill;
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
                    if (skillName[numType].isVisible()) {
                        Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                        if (skill == null) {
                            return;
                        }
                        MsgJframe
                                .getJframe()
                                .getJapnel()
                                .SM(skill, Double.parseDouble(skillValue[numType].getText().split("/")[0]),
                                        AnalysisString.lvlint(RoleData.getRoleData().getLoginResult().getGrade()));
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    FormsManagement.HideForm(46);
                }
            }

            for (int i = 0; i < skillShelter.length; i++) {
                MListener1 l = new MListener1(i);
                skillskilimg[i].addMouseListener(l);
            }
        }else{
            this.setBounds(176 * py, 0, 170, 309);
            this.setOpaque(false);
            this.setLayout(null);
            // 生成有下划线的字体
            HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
            //hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); // 定义是否有下划线
            hm.put(TextAttribute.SIZE, 12); // 定义字号
            hm.put(TextAttribute.FAMILY, "Simsun"); // 定义字体名
            Font fontAddr = new Font(hm);
            // 组件初始化
            skillTitle = new JLabel("速");
            skillSects = new JLabel("门派:");
            skillAddress = new JLabel("长生殿");
            skillBar = new JLabel[5];
            skillValue = new JLabel[5];
            skillArt = new JLabel[5];
            skillName = new JLabel[5];
            // skillClick = new JLabel[5];
            skillShelter = new JLabel[5];
            skillskilimg = new JLabel[5];
            for (int i = 0; i < 5; i++) {
                skillBar[i] = new JLabel();
                skillValue[i] = new JLabel(i + "/100000");
                skillArt[i] = new JLabel();
                skillShelter[i] = new JLabel("点击学习该技能");
                skillName[i] = new JLabel("技能名称");
                skillskilimg[i] = new JLabel();
            }

            // 组件设置字体颜色
            skillTitle.setForeground(new Color(251, 216, 51));
            skillSects.setForeground(new Color(251, 216, 51));
            skillAddress.setForeground(new Color(0, 255, 0));
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setForeground(Color.white);
                skillName[i].setForeground(Color.white);
                skillShelter[i].setForeground(Color.green);
            }

            // 组件设置字体大小样式
            skillTitle.setFont(UIUtils.TEXT_HY34);
            skillSects.setFont(new Font("宋体", 0, 12));
            skillAddress.setFont(fontAddr);
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setFont(new Font("宋体", 0, 12));
                skillName[i].setFont(fontAddr);
                skillShelter[i].setFont(fontAddr);
            }

            // 设置图片
            for (int i = 0; i < 5; i++) {
                this.skillBar[i].setIcon(CutButtonImage.getImage("inkImg/hongmu/soaringSkill/技能框w148,h18px.png", 108, 18));
                this.skillArt[i].setIcon(CutButtonImage.getImage("inkImg/hongmu/soaringSkill/技能条w140,h8px.png", 100, 8));
            }

            // 组件设置大小和方位
            skillTitle.setBounds(0, 0, 170, 40);
            skillSects.setBounds(45, 35, 30, 15);
            skillAddress.setBounds(75, 35, 90, 15);
            for (int i = 0; i < 5; i++) {
                skillBar[i].setBounds(51, 78 + i * 49, 108, 18);
                skillArt[i].setBounds(54, 82 + i * 49, 101, 8);
                skillValue[i].setBounds(53, 79 + i * 49, 103, 12);
                skillName[i].setBounds(51, 60 + i * 49, 103, 14);
                skillShelter[i].setBounds(45, 59 + i * 49, 119, 39);
//    			skillskilimg[i].setBounds(5, 59 + i * 49, 1519, 39);
            }

            // 组建设置透明
            skillTitle.setOpaque(false);
            skillSects.setOpaque(false);
            skillAddress.setOpaque(false);
            for (int i = 0; i < 5; i++) {
                skillBar[i].setOpaque(false);
                skillArt[i].setOpaque(false);
                skillValue[i].setOpaque(false);
                skillName[i].setOpaque(false);
                skillShelter[i].setOpaque(false);
                skillskilimg[i].setOpaque(false);
            }
            // 内容居中
            skillTitle.setHorizontalAlignment(SwingConstants.CENTER);
            for (int i = 0; i < skillValue.length; i++) {
                skillValue[i].setHorizontalAlignment(SwingConstants.CENTER);
                skillShelter[i].setHorizontalAlignment(SwingConstants.CENTER);
            }

            for (int i = 0; i < skillShelter.length; i++) {
                skillShelter[i].setVisible(true);
            }
            // 添加组件
            this.add(skillTitle);
            this.add(skillSects);
            this.add(skillAddress);
            for (int i = 0; i < 5; i++) {
                this.add(skillName[i]);
                this.add(skillValue[i]);
                this.add(skillArt[i]);
                this.add(skillBar[i]);
                this.add(skillShelter[i]);
                this.add(skillskilimg[i]);
            }
            skillAddress.addMouseListener(new MouseAdapter() {
                Cursor cursor = new Cursor(Cursor.HAND_CURSOR);

                @Override
                public void mouseEntered(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseEntered(e);
                    JLabel source = (JLabel) e.getSource();
                    source.setCursor(cursor);
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseClicked(e);
                    JLabel source = (JLabel) e.getSource();
                    source.setCursor(cursor);
                }
            });
            class MListener extends MouseAdapter {
                private int numType;

                public MListener(int numType) {
                    this.numType = numType;
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (!skillName[numType].isVisible()) {
                        MapAction.npcmenuAction.get(NpcMenuUntil.SKILL).menuControl(skillName[numType].getText());
                        SkillMainFrame.getSkillMainFrame().getSkillMainPanel().changeBtnPanel(0);
                    }
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    if (skillName[numType].isVisible()) {
                        Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                        if (skill == null) {
                            return;
                        }
                        MsgJframe
                                .getJframe()
                                .getJapnel()
                                .SM(skill, Double.parseDouble(skillValue[numType].getText().split("/")[0]),
                                        AnalysisString.lvlint(RoleData.getRoleData().getLoginResult().getGrade()));
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    FormsManagement.HideForm(46);
                }
            }

//            for (int i = 0; i < skillShelter.length; i++) {
//                MListener l = new MListener(i);
//                skillShelter[i].addMouseListener(l);
//                skillName[i].addMouseListener(l);
//
//            }
            for (int i = 0; i < skillShelter.length; i++) {
                MListener l = new MListener(i);
                skillShelter[i].addMouseListener(l);
                skillName[i].addMouseListener(l);
                skillBar[i].addMouseListener(l);
                skillArt[i].addMouseListener(l);
                skillValue[i].addMouseListener(l);
            }
            class MListener1 extends MouseAdapter {
                private int numType;

                public MListener1(int numType) {
                    this.numType = numType;
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                    GameJpanel.skill = skill;
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
                    if (skillName[numType].isVisible()) {
                        Skill skill = UserMessUntil.getskill1(skillName[numType].getText());
                        if (skill == null) {
                            return;
                        }
                        MsgJframe
                                .getJframe()
                                .getJapnel()
                                .SM(skill, Double.parseDouble(skillValue[numType].getText().split("/")[0]),
                                        AnalysisString.lvlint(RoleData.getRoleData().getLoginResult().getGrade()));
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    FormsManagement.HideForm(46);
                }
            }

            for (int i = 0; i < skillShelter.length; i++) {
                MListener1 l = new MListener1(i);
                skillskilimg[i].addMouseListener(l);
            }
        }


    }

    private static ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if("水墨UI".equals(MyIsif.getStyle())) {
            if (icon == null) {
                icon = CutButtonImage.getImage("inkImg/newbackground/B221.png", -1, -1);
            }
            g.drawImage(icon.getImage(), 0, 0, 170, 300, this);
        }else{
            if (icon == null) {
                icon = new ImageIcon("inkImg/hongmu/soaringSkill/师门w170,h300.png");
            }

            g.drawImage(icon.getImage(), 0, 0, 170, 300, this);
        }

    }

    public JLabel getSkillTitle() {
        return skillTitle;
    }

    public void setSkillTitle(JLabel skillTitle) {
        this.skillTitle = skillTitle;
    }

    public JLabel getSkillSects() {
        return skillSects;
    }

    public void setSkillSects(JLabel skillSects) {
        this.skillSects = skillSects;
    }

    public JLabel getSkillAddress() {
        return skillAddress;
    }

    public void setSkillAddress(JLabel skillAddress) {
        this.skillAddress = skillAddress;
    }

    public JLabel[] getSkillBar() {
        return skillBar;
    }

    public void setSkillBar(JLabel[] skillBar) {
        this.skillBar = skillBar;
    }

    public JLabel[] getSkillValue() {
        return skillValue;
    }

    public void setSkillValue(JLabel[] skillValue) {
        this.skillValue = skillValue;
    }

    public JLabel[] getSkillArt() {
        return skillArt;
    }

    public void setSkillArt(JLabel[] skillArt) {
        this.skillArt = skillArt;
    }

    public JLabel[] getSkillName() {
        return skillName;
    }

    public void setSkillName(JLabel[] skillName) {
        this.skillName = skillName;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        SkillSMPanel.icon = icon;
    }

    public static void setSkillDivisionGateLeftPanel(SkillSMPanel skillDivisionGateLeftPanel) {
    }

    /** 添加名称技能 */
    public void getSkillsName(String[] skillString, int lvltrue, String[] vs) {
        switch (lvltrue) {
        case 0:
            proficiencyMax = 10000;
            break;
        case 1:
            proficiencyMax = 15000;
            break;
        case 2:
            proficiencyMax = 20000;
            break;
        case 3:
        case 4:
            proficiencyMax = 25000;
            break;
        default:
            break;
        }
        for (int i = 0; i < skillString.length; i++) {
        	skillskilimg[i].setBounds(8, 58+49*i, 40, 40);
            skillName[i].setText(skillString[i]);
            skillShelter[i].setVisible(true);
            skillskilimg[i].setVisible(true);
        }
        if (vs != null) {
            for (int i = 0; i < vs.length; i++) {
                String[] vss = vs[i].split("_");
                Skill skill = UserMessUntil.getSkillId(vss[0]);
                if (skill != null) {
                    for (int j = 0; j < skillString.length; j++) {
                        if (skillString[j].equals(skill.getSkillname())) {
                            skillValue[j].setText(vss[1] + "/" + proficiencyMax);
                            int parseInt = Integer.parseInt(vss[1]);
                            int witNum = (int) Math.round(Arith.mul(
                                    Arith.div(Double.valueOf(parseInt), Double.valueOf(proficiencyMax), 1), 140));
                            witNum = witNum > 0 ? witNum : 1;
                            witNum = witNum >= 100 ? 100 : witNum;
                            skillArt[j].setSize(witNum, skillArt[j].getHeight());
                            skillArt[j]
                                    .setIcon(CutButtonImage.getImage("inkImg/hongmu/soaringSkill/技能条w140,h8px.png", witNum, 8));
                            skillShelter[j].setVisible(false);
                            skillskilimg[j].setIcon(CutButtonImage.getImage("inkImg/old/skill/fighting-skill/"+skill.getSkillid()+".png", 40, 40));
                            break;
                        }
                    }
                }
            }
        }
        // for (int i = 0; i < skillString.length; i++) {
        // skillName[i].setText(skillString[i]);
        // for (int j = 0; j < split.length; j++) {
        // String[] skill = split[j].split("*");
        // Skill skill2=UserMessUntil.getSkillId(skill[0]);
        // if(skillString[i].equals(skill[0])){
        // skillValue[i].setText(skill[1]+"/" + proficiencyMax);
        // int parseInt = Integer.parseInt(skill[1]);
        // // int witNum = parseInt/proficiencyMax*skillArt[i].getWidth();
        // int witNum = (int)
        // Math.round(Arith.mul(Arith.div(Double.valueOf(parseInt),
        // Double.valueOf(proficiencyMax), 1), 140));
        // witNum = witNum > 0 ? witNum : 1 ;
        // witNum = witNum >= 140 ? 140 : witNum ;
        // skillArt[i].setSize(witNum, skillArt[i].getHeight());
        // skillArt[i].setIcon(CutButtonImage.getImage("inkImg/hongmu/soaringSkill/技能条w140,h8px.png",
        // witNum, 8));
        // skillShelter[i].setVisible(false);
        // }
        // }
        // }
        changeViewTrue();
    }

    public void changeViewTrue() {
        for (int i = 0; i < skillName.length; i++) {
            skillName[i].setVisible(!skillShelter[i].isVisible());
            skillValue[i].setVisible(!skillShelter[i].isVisible());
            skillArt[i].setVisible(!skillShelter[i].isVisible());
            skillBar[i].setVisible(!skillShelter[i].isVisible());
            skillskilimg[i].setVisible(!skillShelter[i].isVisible());
//             skillClick[i].setVisible(!skillShelter[i].isVisible());
        }
    }
}
